package parser;

import messages.AddBuddyMessage;
import messages.AwayMessage;
import messages.ChatMessage;
import messages.GetBuddyListMessage;
import messages.LoginMessage;
import messages.RegistrationMessage;
import messages.RemoveBuddyMessage;
import messages.ReturnAvailabilityMessage;
import messages.ReturnStatusMessage;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Parser
{
	public Parser() {}
	
	public LoginMessage getLoginMessage(Element docEle)
	{
		// get the "username" tag
		//NodeList userTag = docEle.getElementsByTagName("username");
		//Element user = (Element)userTag.item(0);
		
		String username = getTextValue(docEle,"username");
		
		// get the "password" tag
		//NodeList passTag = docEle.getElementsByTagName("password");
		//Element pass = (Element)passTag.item(0);
		
		String password = getTextValue(docEle,"password");
		
		// get the "version" tag
		//NodeList versionTag = docEle.getElementsByTagName("version");
		//Element ver = (Element)versionTag.item(0);
		
		String version = getTextValue(docEle,"version");
		
		// construct a LoginMessage and return it
		LoginMessage loginMessage = new LoginMessage(username,password,version);
		return loginMessage;
	}
	
    public ChatMessage getChatMessage(Element docEle)
    {
    	// get the "from" tag
		NodeList users = docEle.getElementsByTagName("from");
		Element userFrom = (Element)users.item(0);
		
		// get the "to" tag
		NodeList users2 = docEle.getElementsByTagName("to");
		Element userTo = (Element)users2.item(0);
		
		// get the "username" tag values from the "from" and "to" tags
		String from = getTextValue(userFrom,"username");
		String to = getTextValue(userTo,"username");
		
		// get the "timestamp" tag value
		long timestamp = System.currentTimeMillis();
		
		// get the "text" tag value
		String text = getTextValue(docEle, "text");
		
		// construct a ChatMessage and return it
		ChatMessage chatMessage = new ChatMessage(from,to,text);
		return chatMessage;
    }
    
    public AwayMessage getAwayMessage(Element docEle)
    {
    	// get the "from" tag
		NodeList users = docEle.getElementsByTagName("from");
		Element userFrom = (Element)users.item(0);
		
		// get the "to" tag
		NodeList users2 = docEle.getElementsByTagName("to");
		Element userTo = (Element)users2.item(0);
		
		// get the "username" tag values from the "from" and "to" tags
		String from = getTextValue(userFrom,"username");
		String to = getTextValue(userTo,"username");
		
		// get the "timestamp" tag value
		long timestamp = System.currentTimeMillis();
		
		// get the "text" tag value
		String text = getTextValue(docEle, "text");
		
		// construct a ChatMessage and return it
		AwayMessage awayMessage = new AwayMessage(from,to,text);
		return awayMessage;
    }
    
    public AddBuddyMessage getAddBuddyMessage(Element docEle)
    {
    	// get the "from" tag
		NodeList user = docEle.getElementsByTagName("from");
		Element userFrom = (Element)user.item(0);
		
		// get the "to" tag
		NodeList user2 = docEle.getElementsByTagName("to");
		Element userTo = (Element)user2.item(0);
		
		// get the "username" tag values from the "from" and "to" tags
		String from = getTextValue(userFrom,"username");
		String to = getTextValue(userTo,"username");
				
		// get the "text" tag value
//		String text = getTextValue(docEle, "text");
		
		// construct a AddBuddyMessage and return it
		AddBuddyMessage adMessage = new AddBuddyMessage("AddBuddy",from,to);
		return adMessage;
    }
    
    public RemoveBuddyMessage getRemoveBuddyMessage(Element docEle) {
    	// get the "from" tag
		NodeList user = docEle.getElementsByTagName("from");
		Element userFrom = (Element)user.item(0);
		
		// get the "to" tag
		NodeList user2 = docEle.getElementsByTagName("to");
		Element userTo = (Element)user2.item(0);
		
		// get the "username" tag values from the "from" and "to" tags
		String from = getTextValue(userFrom,"username");
		String to = getTextValue(userTo,"username");
				
		// get the "text" tag value
//		String text = getTextValue(docEle, "text");
		
		// construct a AddBuddyMessage and return it
		RemoveBuddyMessage rmMessage = new RemoveBuddyMessage("RemoveBuddy",from,to);
		return rmMessage;
	}
    
    public ReturnStatusMessage getReturnStatusMessage(Element docEle) {
    	// get the "from" tag
		NodeList userTag = docEle.getElementsByTagName("username");
		Element user = (Element)userTag.item(0);
		
		// get the "to" tag
		NodeList statusTag = docEle.getElementsByTagName("status");
		Element status = (Element)statusTag.item(0);
		
		// get the "username" tag values from the "from" and "to" tags
		String userstr = getTextValue(user,"username");
		String statusstr = getTextValue(status,"status");
				
		// get the "text" tag value
		// String text = getTextValue(docEle, "text");
		
		// construct a AddBuddyMessage and return it
		if(statusstr == null || statusstr.equals("")) {statusstr = "1";}
		ReturnStatusMessage rsMessage = new ReturnStatusMessage("ReturnStatus", userstr, Integer.parseInt(statusstr));
		return rsMessage;
	}
    

    public GetBuddyListMessage getBuddyListMessage(Element docEle) {
    	// get the "from" tag
//		NodeList userTag = docEle.getElementsByTagName("username");
//		Element user = (Element)userTag.item(0);
		
		// get the "username" tag values from the "from" and "to" tags
//		String userstr = getTextValue(user,"username");
		String user = getTextValue(docEle, "username");
//		System.out.println(user);
				
		// get the "text" tag value
//		String text = getTextValue(docEle, "text");
		
		// construct a AddBuddyMessage and return it
		GetBuddyListMessage gblMessage = new GetBuddyListMessage("GetBuddyList", user);
		return gblMessage;
    }
    
    public RegistrationMessage getRegistrationMessage(Element docEle) {
    	
    	String userstr = getTextValue(docEle, "username");
    	String passwordstr = getTextValue(docEle, "password");
    	
    	RegistrationMessage regMessage = new RegistrationMessage("Registration", userstr, passwordstr);
    	return regMessage;
    }
    
    public ReturnAvailabilityMessage getReturnAvailabilityMessage(Element docEle)
    {
    	NodeList userTag = docEle.getElementsByTagName("user");
		Element user = (Element)userTag.item(0);
		
		
		NodeList availabilityTag = docEle.getElementsByTagName("availability");
		Element availability = (Element)availabilityTag.item(0);
		
		String userstr = getTextValue(user,"username");
		String availabilitystr = getTextValue(availability,"availability");
		
		ReturnAvailabilityMessage raMessage = new ReturnAvailabilityMessage("UpdateAvailability", userstr, availabilitystr);
		return raMessage;
    }
    
    
    /**
	 * I take a xml element and the tag name, look for the tag and get
	 * the text content 
	 * i.e for <employee><name>John</name></employee> xml snippet if
	 * the Element points to employee node and tagName is name I will return John  
	 * @param ele
	 * @param tagName
	 * @return
	 */
    
	public String getTextValue(Element ele, String tagName) {
		String textVal = null;
		NodeList nl = ele.getElementsByTagName(tagName);
		if(nl != null && nl.getLength() > 0) {
			Element el = (Element)nl.item(0);
			textVal = el.getFirstChild().getNodeValue();
		}

		return textVal;
	}

	
	/**
	 * Calls getTextValue and returns a int value
	 * @param ele
	 * @param tagName
	 * @return
	 */
	public int getIntValue(Element ele, String tagName) {
		//in production application you would catch the exception
		return Integer.parseInt(getTextValue(ele,tagName));
	}
	
}
